package com.sali.commons.config;


import com.sali.commons.interceptor.permission.SecurityInterceptor;
import com.sali.commons.interceptor.auth.AdminLoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
* description: 添加拦截器
**/
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Autowired
    private SecurityInterceptor securityInterceptor;

    @Autowired
    private AdminLoginInterceptor adminLoginInterceptor;


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 允许跨域的路径
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE","OPTIONS") // 允许的请求方法
                .allowedHeaders("*") // 允许的请求头
                .allowCredentials(true); // 是否允许证书（cookies）
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(adminLoginInterceptor).addPathPatterns("/admin/v1/**").excludePathPatterns(
                "/admin/v1/login/toLogin",
                "/admin/v1/code/get");
        registry.addInterceptor(securityInterceptor).addPathPatterns("/admin/v1/**").excludePathPatterns(
                "/admin/v1/login/toLogin",
                "/admin/v1/login/logout",
                "/admin/v1/role/change",
                "/admin/v1/code/get");
    }
}
